package com.eke.dao;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eke.bean.Article;
import com.eke.bean.QuestionFrom;
import com.jason.dbhelper2.jdbc.BaseJdbcTemplate;

@Repository("articleDao")
public class ArticleDao extends BaseJdbcTemplate<Article> {
	/**
	 * 增加评论人数
	 * 
	 * @param articleId
	 *            文章或问题ID
	 * @return int
	 */
	public int addCommentNumber(int articleId) {
		String sql = "Update article set comment_number = comment_number + 1 where article_id =?";
		return this.update(sql, articleId);
	}

	/**
	 * 添加问题
	 * 
	 * @param article
	 *            问题对象
	 * @return int
	 * 
	 */
	public int addArticle(Article article) {
		return this.insert(article);
	}

	/**
	 * 查询所有文章
	 * 
	 * @param startRow
	 *            开始页
	 * @param pageSize
	 *            数量
	 * @return List<Article>
	 */
	public List<Article> qurryAllArticle(int startRow, int pageSize) {
		String sql = "select * from article WHERE article_type = 1 ORDER BY article_id DESC limit ?,? ";
		return this.queryForList(sql, startRow, pageSize);
	}

	/**
	 * 查询文章总数
	 * 
	 * @return int
	 */
	public int queryArticleTotal() {
		Map<String, Object> map = this.getJdbcTemplate()
				.queryForMap("select count(1) total from article where article_Type=1");
		return ((Long) map.get("total")).intValue();
	}

	/**
	 * 根据文章id查询文章详情
	 * 
	 * @param articleId
	 *            文章id
	 * @return {@link Article}
	 */
	public Article queryArticleDeatilsByArticleId(int articleId) {
		String sql = "select * from article where article_id = " + articleId;
		return this.queryForObjectByNames(sql);
	}

	/*
	 * -------------------------------------------------------------------------
	 * --------
	 */
	/**
	 * 查询所有问题
	 * 
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryAllQuestion(int startRow, int pageSize) {
		String sql = "select * from article where article_type=2 ORDER BY article_id DESC limit ?,? ";
		return this.queryForList(sql, startRow, pageSize);
	}

	/**
	 * 查询问题总数
	 * 
	 * @return int
	 */
	public int queryQuestionTotal() {
		Map<String, Object> map = this.getJdbcTemplate()
				.queryForMap("select count(1) total from article where article_type=2");
		return ((Long) map.get("total")).intValue();
	}

	/*
	 * -------------------------------------------------------------------------
	 * --------
	 */
	/**
	 * 查询13篇文章随机
	 * 
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryTenArticle() {
		return this.queryForList("SELECT * FROM  Article where article_type=1  ORDER BY  RAND() LIMIT 13");
	}

	/*-------------------------------------------------------------------------*/
	/**
	 * 根据用户id查询该用户的所有文章
	 * 
	 * @param user_id
	 *            用户id
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryAllArticleById(Integer userId, int startRow, int pageSize) {
		return this.queryForList(
				"select * from article where user_id = ? and article_type=1 and article_state != 3 limit ?,?", userId,
				startRow, pageSize);
	}

	/**
	 * 根据用户id查询该用户的所有问题
	 * 
	 * @param user_id
	 *            用户id
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryAllQuestionById(Integer userId) {
		return this.queryForList("select * from article where user_id = ? and article_type=2 and article_state != 3",
				userId);
	}

	/**
	 * 根据文章id删除文章
	 * 
	 * @param articleId
	 * @return {@link Boolean}
	 */
	public boolean delectArticleById(Integer articleId) {
		return this.update("update article set article_state = 3 where article_id = ? and article_type = 1",
				articleId) > 0;
	}

	/**
	 * 根据问题id删除问题
	 * 
	 * @param articleId
	 *            问题id
	 * @return {@link Boolean}
	 */
	public boolean delectQuestionById(Integer articleId) {
		return this.update("update article set article_state = 3 where article_id = ? and article_type = 2",
				articleId) > 0;
	}

	/**
	 * 根据文章名查文章
	 * 
	 * @param articleTitle
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryArticleByName(String articleTitle) {
		return this.queryForList(
				"select * from article where (article_state != 3 and article_type = 1) and article_title like '%?%'",
				articleTitle);
	}

	/**
	 * 查询所有文章
	 * 
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryArticle() {
		return this.queryForList("select * from article where article_state != 3 and article_type =1");
	}

	/**
	 * 查询所有问题
	 * 
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryQuestion() {
		return this.queryForList("select * from article where article_state != 3 and article_type =2");
	}

	/**
	 * 查询所有文章ByTitleAndLimit
	 * 
	 * @return List<Article>
	 */
	public List<Article> queryArticleByTitleAndLimit(String articleTitle, int startRow, int pageSize) {
		// String sql = "select * from article WHERE article_state != 3 and
		// article_type = 1 and article_title LIKE '%?%' ORDER BY article_id
		// DESC limit ?,? ";
		try {
			articleTitle = new String(articleTitle.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from article WHERE article_state != 3 and article_type = 1 and article_title LIKE '%"
				+ articleTitle + "%' ORDER BY article_id DESC limit " + startRow + "," + pageSize + " ";
		return this.queryForList(sql);
	}

	/**
	 * 更改问题状态为解决
	 * 
	 * @return {@link Boolean}
	 */
	public boolean updateQuestionState(Integer questionId) {
		return this.update("UPDATE article SET question_state = 0 WHERE article_id = ?", questionId) > 0;
	}

	/**
	 * 通过问题id查询问题
	 * 
	 * @param questionId
	 * @return {@link Article}
	 */
	public Article isCanAgree(Integer questionId) {
		return this.queryForObject("select * from article where article_Id=? ", questionId);
	}

	/**
	 * 查询所有问题ByTitleAndLimit
	 * 
	 * @return @{@link List}<{@link Article}>
	 */
	public List<Article> queryQusetionByTitleAndLimit(String articleTitle, int startRow, int pageSize) {
		// String sql = "select * from article WHERE article_state != 3 and
		// article_type = 1 and article_title LIKE '%?%' ORDER BY article_id
		// DESC limit ?,? ";
		try {
			articleTitle = new String(articleTitle.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from article WHERE article_state != 3 and article_type = 2 and article_title LIKE '%"
				+ articleTitle + "%' ORDER BY article_id DESC limit " + startRow + "," + pageSize + " ";
		return this.queryForList(sql);
	}

	/**
	 * 修改文章状态
	 * 
	 * @param id
	 * @return {@link Boolean}
	 */
	public boolean updateArticleState(Integer id) {
		System.out.println(id);
		Article article = this.queryForObject("select * from article where article_id = ?", id);
		Integer state = article.getArticleState();
		if (state == 1) {
			state = 0;
		} else if (state == 0) {
			state = 1;
		}
		return this.update("update article set article_state = ?", state) > 0;
	}

}
